Hook to fetch current prices for a list of tokens
import { useGetCoinPrices } from '@0xsequence/hooks'
import { useGetCoinPrices } from '@0xsequence/hooks'
import { ZERO_ADDRESS } from '@0xsequence/hooks'
function TokenPriceDisplay() {
const tokens = [
{
chainId: 1,
contractAddress: ZERO_ADDRESS // ETH
},
{
chainId: 137,
contractAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174' // USDC on Polygon
}
]
const {
data: prices,
isLoading,
error,
isError,
isSuccess
} = useGetCoinPrices(tokens)
if (isLoading) {
return <div>Loading prices...</div>
}
if (isError) {
return <div>Error: {error.message}</div>
}
return (
<div>
<h2>Token Prices</h2>
{isSuccess && prices && (
<ul>
{prices.map((price, index) => (
<li key={`${tokens[index].chainId}-${tokens[index].contractAddress}`}>
Chain {tokens[index].chainId}:{' '}
{price.price.value} {price.price.currency}
</li>
))}
</ul>
)}
</div>
)
}
Token[]
Array of tokens to get prices for. Each token must include:
interface Token {
chainId: number // The chain ID where the token exists
contractAddress: string // The token's contract address
}
ZERO_ADDRESS
for native tokens (e.g., ETH on Ethereum, MATIC on Polygon).
HooksOptions
(optional)
interface HooksOptions {
retry?: boolean // Whether to retry failed requests (defaults to true)
disabled?: boolean // Whether to disable the query
}
interface Price {
value: number
currency: string
}
{
data: {
token: Token,
price?: Price,
price24hChange?: Price,
floorPrice?: Price,
buyPrice?: Price,
sellPrice?: Price,
updatedAt: string
}[]
isLoading: boolean // Whether the initial request is in progress
error: Error | null // Any error that occurred
isError: boolean // Whether an error occurred
isSuccess: boolean // Whether the request was successful
}
Was this page helpful?